package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.QuestionDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Question;
import com.clz.hyperit.vo.PageInfo;
@Repository("questionDao")
public class QuestionDaoImpl extends BaseDaoImpl<Question> implements QuestionDao {

	public List<Question> findByuserdetail_id(PageInfo pageInfo,
			Integer userdetail_id) {
		String hql = "from Question where userdetail_id = :userdetail_id order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("userdetail_id", userdetail_id)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question where  userdetail_id = :userdetail_id";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("userdetail_id", userdetail_id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Question> findQuestionByAccept_accept(PageInfo pageInfo) {
		String hql = "from Question where accept <> 0 order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question where accept <> 0";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Question> findQuestionByAccept_notaccept(PageInfo pageInfo) {
		String hql = "from Question where accept = 0 order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question where accept = 0";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Question> getQuestionByAnswer(PageInfo pageInfo) {
		String hql = "from Question order by answer desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Question> getQuestionByRead(PageInfo pageInfo) {
		String hql = "from Question order by reading desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Question> SearchQuestion(PageInfo pageInfo, String search) {
		String hql = "from Question where title like :search order by reading desc";
		@SuppressWarnings("unchecked")
		List<Question> list = getCurrentSession()
				.createQuery(hql)
				.setString("search","%"+search+"%")
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Question where title like :search";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setString("search","%"+search+"%")
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

}
